<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <div id="root">
        <h2 :style="{opacity: opacity}">欢迎学习Vue</h2>
        <button @click="opacity = 1">透明度变成1</button>
        <button @click="stop">点我停止变换</button>
    </div>
</body>

</html>
<script>
    const vm = new Vue({
        el: '#root',
        data: {
            opacity: 1
        },
        methods: {
            stop() {
                // clearInterval(this.timer)
                this.$destroy()
            }
        },
        //Vue完成模板的解析并把初始的真实的DOM放入页面后(挂载完毕)调用mounted
        mounted() {
            this.timer = setInterval(() => {
                this.opacity -= 0.01
                if (this.opacity <= 0) this.opacity = 1
            }, 16);
        },
    })

</script>